﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

using FRL.DebtChased.Repository;
using FRL.DebtChased.Website;
using DevMessageEnt = FRL.DebtChased.Repository.DevMessage;

namespace WebApplication.Finance
{

	/// <summary>
	/// Declaration of DevMessage
	/// </summary>
	public partial class DevMessage : System.Web.UI.Page
	{

		/////////////////////////////////////////////////////////////
		// Event handlers

		protected void Page_Load(object sender, EventArgs e)
		{

			// If this is a postback...
			if (this.IsPostBack)
			{

			}
			else
			{

				// Get the source page
				lbPage.Text = Request["url"];
				if(string.IsNullOrEmpty(lbPage.Text))
					lbPage.Text = Request.ServerVariables["HTTP_REFERER"];

				// Set controls - priority and category
				cboPriority.BindAndSelected(typeof(DevMessageEnt.ePriority), (int)DevMessageEnt.ePriority.Medium);
				cboCategory.BindAndSelected(typeof(DevMessageEnt.eCategory), (int)DevMessageEnt.eCategory.Bug);

			}

		}

		protected void butSave_Click(object sender, EventArgs e)
		{

			// Validate content
			this.Validate();
			if(!this.IsValid)
				return;

			// Declare an entities object
			using (DebtChasedEntities ent = new DebtChasedEntities())
			{

				// Add to entities
				FRL.DebtChased.Repository.DevMessage.Save(
					ent,
					MySession.BindMySession().UserID,
					lbPage.Text,
					txtDescription.Text,
					(DevMessageEnt.ePriority)int.Parse(cboPriority.SelectedValue),
					(DevMessageEnt.eCategory)int.Parse(cboCategory.SelectedValue)
				);

				// Update message
				(this.Master as FinanceMaster).ErrorMessage = "Dev Message has been saved to the database.";

			}

		}

	}

}